package com.lijian.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lijian.common.easyui.tree.EasyuiTreeNode;
import com.lijian.common.utils.TaotaoResult;
import com.lijian.mapper.TbContentCategoryMapper;
import com.lijian.pojo.TbContentCategory;
import com.lijian.pojo.TbContentCategoryExample;
import com.lijian.pojo.TbContentCategoryExample.Criteria;
import com.lijian.service.ContentCategoryService;

/**
 * <p>Title: ContentCategoryServiceImpl.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2017</p>
 * @author lijian
 * @date 2017年4月15日上午10:28:30
 * @version 1.0
 */
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

	@Autowired
	private TbContentCategoryMapper contentCategoryMapper;
	@Override
	public List<EasyuiTreeNode> getContentCategoryList(Long parentId) {
		TbContentCategoryExample example=new TbContentCategoryExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andParentIdEqualTo(parentId);
		java.util.List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
		List<EasyuiTreeNode> res=new ArrayList();
		
		for(TbContentCategory item:list){
			EasyuiTreeNode node=new EasyuiTreeNode();
			node.setId(item.getId());
			node.setText(item.getName());
			node.setState(item.getIsParent()?"closed":"open");
			res.add(node);
		}
		
		
		return res;
	}
	@Override
	public TaotaoResult insertCategory(Long parentId, String name) {
		TbContentCategory record=new TbContentCategory();
		record.setParentId(parentId);
		record.setName(name);
		record.setCreated(new Date());
		record.setUpdated(new Date());
		record.setIsParent(false);
		record.setStatus(1);
		record.setSortOrder(1);
		contentCategoryMapper.insert(record);
		Long id=record.getId();
		
		//更新parentId 为非叶子节点
		TbContentCategory parentNode = contentCategoryMapper.selectByPrimaryKey(parentId);
		if(!parentNode.getIsParent()){
			parentNode.setIsParent(true);
			contentCategoryMapper.updateByPrimaryKey(parentNode);
		}
		
		
		return TaotaoResult.ok(id);
	}

}
